<template>
  <div>
    <el-row>
      <el-col :span="6">
        <el-date-picker
          v-model="timeRange"
          @change="changeTime"
          type="datetimerange"
          :shortcuts="shortcuts"
          range-separator="至"
          start-placeholder="请选择开始时间"
          end-placeholder="请选择结束时间"
        >
        </el-date-picker>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column prop="createTime" label="下载时间" />
          <el-table-column prop="oldFileName" label="原文件名称" />
          <el-table-column prop="downloadFileName" label="下载文件名称" />
          <el-table-column prop="ip" label="ip" /> </el-table
      ></el-col>
    </el-row>
    <div class="block">
      <el-pagination
        layout="prev, pager, next"
        @current-change="handleCurrentChange"
        :total="page.total"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import { onMounted, reactive, toRefs } from "vue";
import { downloadHistoryList } from "@/api/article.js";
export default {
  setup() {
    const data = reactive({
      shortcuts: [
        {
          text: "近一周",
          value: () => {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            return [start, end];
          },
        },
        {
          text: "近一个月",
          value: () => {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            return [start, end];
          },
        },
        {
          text: "近三个月",
          value: () => {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            return [start, end];
          },
        },
      ],
      tableData: [],
      timeRange: [],
      page: {
        pageSize: 10,
        pageNum: 1,
        total: 0,
      },
    });
    onMounted(() => {
      getData();
    });
    function changeTime() {
      getData();
    }
    function handleCurrentChange(e) {
      data.page.pageNum = e;
      getData();
    }
    function getData() {
      let startTime = "",
        endTime = "";
      if (data.timeRange) {
        [startTime = "", endTime = ""] = data.timeRange;
      }
      downloadHistoryList({
        startTime: startTime ? new Date(startTime).getTime() : startTime,
        endTime: endTime ? new Date(endTime).getTime() : endTime,
        pageSize: data.page.pageSize,
        pageNum: data.page.pageNum,
      })
        .then((res) => {
          if (res.code == 200) {
            data.tableData = res.data.data;
            data.page.total = res.data.total;
          }
        })
        .catch(() => {});
    }
    return { ...toRefs(data), handleCurrentChange, changeTime };
  },
};
</script>

<style lang="scss" scoped>
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.block {
  text-align: center;
}
</style>
